import React from 'react';
import { connect } from 'dva';
import {Form,Row,Col,DatePicker,Table,Pagination,Input,Button,Select} from 'antd';
const dateFormat="YYYY-MM-DD";
const {RangePicker}=DatePicker;
const FormItem=Form.Item;
const R=require("ramda");
const Option=Select.Option;

    const orderReport = Form.create()(class OrderReport extends React.Component{
        constructor(props){
            super(props)
        }
        columns=[
            {title:"客户名称",dataIndex:'companyname',key:'companyname'},
            {title:"订单号",dataIndex:'orderno',key:'orderno'},
            {title:"合同协议号",dataIndex:'contractno',key:'contractno'},
            {title:"报关方式",dataIndex:'declarationtype',key:'declarationtype'},
            {title:"报关口岸",dataIndex:'placename',key:'placename'},
            {title:"报关币种",dataIndex:'currencytype',key:'currencytype'},
            {title:"报关金额",dataIndex:'totleamount',key:'totleamount'},
            {title:"放行时间",dataIndex:'passtime',key:'passtime'},
            {title:"业务员",dataIndex:'manager',key:'manager'},
            {title:"状态",dataIndex:'status',key:'status'},
            {title:"创建人",dataIndex:'username',key:'username'},
            {title:"创建时间",dataIndex:'createtime',key:'createtime'},

        ];




        handleSearch=()=>{
            let values=this.props.form.getFieldsValue();
            let lesseesCode=values.lesseesCode;
            let times=values.timeRange;
            let status=values.status;
            this.props.dispatch({
                type:"orderReport/getOrderList",
                payload:{
                    beginTime:R.isNil(times)||R.isEmpty(times)?null:times[0].format(dateFormat),
                    endTime:R.isNil(times)||R.isEmpty(times)?null:times[1].format(dateFormat),
                    status:R.isNil(status)?null:status,
                }
            })

        }

        handleExport=()=>{
            let values=this.props.form.getFieldsValue();
            let times=values.timeRange;
            let status=values.status;
            this.props.dispatch({
                type:"orderReport/exportOrderList",
                payload:{
                    beginTime:R.isNil(times)||R.isEmpty(times)?null:times[0].format(dateFormat),
                    endTime:R.isNil(times)||R.isEmpty(times)?null:times[1].format(dateFormat),
                    status:R.isNil(status)?null:status,
                }
            })
        }


        render(){


            let pagination={

                showTotal: total => `共 ${total} 条数据`,
                pageSize: this.props.orderReport.page.size,
                total: this.props.orderReport.page.totalRow,
                showSizeChanger: true,
                current:this.props.orderReport.page.page,
                onShowSizeChange: (current, size) => {
                    let values=this.props.form.getFieldsValue();
                    let times=values.timeRange;
                    let status=values.status;
                    let newPage = this.props.orderReport.page;
                    newPage.size = size;
                    newPage.page = 1;
                    this.props.dispatch({
                        type: 'orderReport/getOrderList',
                        payload: {
                            beginTime:R.isNil(times)||R.isEmpty(times)?null:times[0].format(dateFormat),
                            endTime:R.isNil(times)||R.isEmpty(times)?null:times[1].format(dateFormat),
                            status:R.isNil(status)?null:status,
                            page:newPage,
                        }
                    });
                },
                onChange: (pageNumber, pageSize) => {
                    let values=this.props.form.getFieldsValue();
                    let times=values.timeRange;
                    let newPage = this.props.orderReport.page;
                    let status=values.status;
                    newPage.page = pageNumber;
                    newPage.pageSize=pageSize;
                    this.props.dispatch({
                        type: 'orderReport/getOrderList',
                        payload: {
                            beginTime:R.isNil(times)||R.isEmpty(times)?null:times[0].format(dateFormat),
                            endTime:R.isNil(times)||R.isEmpty(times)?null:times[1].format(dateFormat),
                            status:R.isNil(status)?null:status,
                            page:newPage,
                        }
                    });
                }


            };
            let dataSource=R.isNil(this.props.orderReport.orderList)?null:this.props.orderReport.orderList;
            const {getFieldDecorator}=this.props.form;
            let formItemLayout = { labelCol: { span: 6 }, wrapperCol: { span: 18 } };

            // console.log(this.props);
            return(
             <Form>
                    <Row>
                        <Col span={6}>

                            <FormItem span={6} {...formItemLayout} label="创建时间">
                                {getFieldDecorator("timeRange",{
                                    initialValue:null,
                                })(
                                    <RangePicker
                                        style={{width:230}}
                                        showTime={{format:'YYYY-MM-DD'}}
                                        format="YYYY-MM-DD"
                                        placeholder={['开始时间','结束时间']}
                                    />
                                )
                                }
                            </FormItem>
                        </Col>
                        <Col span={4}>
                            <FormItem {...formItemLayout} span={6} label="状态">
                                {getFieldDecorator("status",{
                                })(
                                    <Select style={{width:120}}>
                                        <Option value={null}>（空）</Option>
                                        <Option value="1">草稿</Option>
                                        <Option value="2">待通关审核</Option>
                                        <Option value="3">待补充资料</Option>
                                        <Option value="4">待签确认函</Option>
                                        <Option value="5">待报关</Option>
                                        <Option value="6">报关中</Option>
                                        <Option value="7">办理完毕</Option>

                                    </Select>
                                )}
                            </FormItem>
                        </Col>

                        <Col span={2}>
                            <Button  type="primary" icon="search" onClick={this.handleSearch.bind(this)}>查询</Button>
                        </Col>
                        <Col span={2}>
                            <Button  type="primary" onClick={this.handleExport.bind(this)}>导出数据</Button>
                        </Col>
                    </Row>

                    <Row>
                        <Table dataSource={dataSource} pagination={pagination} size={'middle'} loading={this.props.loading} columns={this.columns} bordered={true}/>
                    </Row>

                </Form>

            )

        }

    })


function mapStateToProps(state) {
    return {
        loading: state.loading.models.orderReport, orderReport: state.orderReport
    };
}

export default connect(mapStateToProps)(orderReport);